//
//  THTableViewController.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "THTableViewController.h"
#import "UIScrollView+InfiniteScroll.h"

@interface THTableViewController ()

/// viewModel
@property (nonatomic,retain,readwrite) THTableViewModel *viewModel;
/// tableView
@property (nonatomic,retain,readwrite) UITableView *tableView;

@end

@implementation THTableViewController
@dynamic viewModel;

- (void)viewDidLoad{
    [super viewDidLoad];
    [self registerTableViewCell];
}
- (void)setupUI{
    [super setupUI];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = ColorFromHex(0xF5F7F9);
}
- (void)setupFrames{
    [super setupFrames];
    UIEdgeInsets insets = self.viewModel.contentInsets;
    if (self.viewModel.insetsTop    != 0.0)
        insets.top    = self.viewModel.insetsTop;
    if (self.viewModel.insetsLeft   != 0.0)
        insets.left   = self.viewModel.insetsLeft;
    if (self.viewModel.insetsRight  != 0.0)
        insets.right  = self.viewModel.insetsRight;
    if (self.viewModel.insetsBottom != 0.0)
        insets.bottom = self.viewModel.insetsBottom;
    [self.tableView  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(insets);
    }];
}
- (void)afterLoad{
    [super afterLoad];
    if (self.viewModel.pullRefreshEnable) {
        @weakify(self);
        [self.tableView pullupHeaderRefresh:^{
            @strongify(self);
            if (!self) return;
            //下拉刷新
              [self refreshViewHadEndWithIsHeader:YES];
        }];
    }
    if (self.viewModel.infiniteReFreshEnable) {
        @weakify(self);
        
        [self.tableView pullDownFootRefresh:^{
            @strongify(self);
            if (!self) return;
            //上拉加载更多数据
             [self refreshViewHadEndWithIsHeader:NO];
        }];
    }
}
#pragma mark registerTableViewCell Method
- (void)registerTableViewCell{
    //子类重写
}

#pragma mark
- (void)refreshViewHadEndWithIsHeader:(BOOL)isHeader{
    //子类重写
}

#pragma mark 重写showEmptyView
- (void)showEmptyView{
    [self.tableView addSubview:self.emptyView];
}

#pragma mark  tableVieDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return nil;
}
#pragma mark lazy method
- (UITableView*)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:self.viewModel.tableViewStyle];
        _tableView.delegate   = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.estimatedRowHeight = 0.0f;
        _tableView.estimatedSectionFooterHeight = 0.0f;
        _tableView.estimatedSectionHeaderHeight = 0.0f;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
    }
    return _tableView;
}
@end
